<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**json返回值
 * @param string $msg
 * @param int $code
 * @param null $data
 * @return \think\response\Json
 */
function json_to_respos($msg='成功',$code=200,$data=null){
    return json(['msg'=>$msg,'code'=>$code,'data'=>$data]);
}

/**验证
 * @param array $data
 * @param array $array
 * @return mixed
 */
function yanzhen($data=[],$array=[]){
    $datas =\think\Validate::make($array);
    if(!$datas->check($data)){
        $val['code'] = false;
        $val['msg'] = $datas->getError();
    }else{
        $val['code'] = true;
    }
    return $val;
}

/**加密
 * @param $str
 * @param bool $haxi
 * @return bool|string
 */
function pwd_encode($str,$haxi=true){
    if($haxi){
        return password_hash($str,PASSWORD_DEFAULT);
    }else{
        return md5(md5($str));
    }
}

/**解密
 * @param $str
 * @param $passworder
 * @param bool $haxi
 * @return bool
 */
function pwd_decode($str,$passworder,$haxi=true){
    if($haxi){
        return password_verify($str,$passworder);
    }else{
        if(md5(md5($str))==$passworder){
            return true;
        }else{
            return false;
        }
    }
}

/**获取配置
 * @param $str 配置英文名
 * @param string类型【value img】 $type
 * @param string 查询的字段 $field
 * @param null 是否指定table $table
 * @return array|bool|mixed|string
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function getSysTem($str,$type='value',$field='en_name',$table=null,$defult=null){
    $data = \think\Db::name(getTable('systems',$table))
        ->where($field,$str)
        ->find();
    if($data){
        $res['value'] = $data['value'];
        $res['img'] = getFilePath($data['tupian']);
        if($type=='value'){
            return $res['value'];
        }elseif ($type=='img'){
            return $res['img'];
        }
    }else{
        if($type=='value'){
            if($defult||$defult==0){
                return $defult;
            }
            return '';
        }else{
//            if($defult||$defult==0){
//                return $defult;
//            }
            return getFilePath(0);
        }
    }
}

/**获取图片的url地址
 * @param $id 附件表id
 * @param bool 是否返回数组 $isarray
 * @param null 是否指定table $table
 * @return array|bool|mixed|string
 * @throws \think\Exception
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
function getFilePath($id,$isarray=false,$table=null,$defult=null){
    $data = \think\Db::name(getTable('enclosure',$table))
        ->where('id','in',$id)
        ->select();
    if($data){
        $img = [];
        foreach ($data as $value){
            if(!strstr($value['path'],'http')){
                $value['path'] = request()->domain().$value['path'];
            }
            $img[] = $value['path'];
        }
        if($isarray){
            return $img;
        }else{
            return $img[0];
        }
    }else{
        if($defult){
            return $defult;
        }else{
            return request()->domain().'/static/admin/img/error.jpg';
        }
//        if($id){
//            return request()->domain().$id;
//        }else{
//            return request()->domain().'/static/admin/img/code.png';
//        }
    }
}
/**返回json给layui的分页
 * @param $data
 * @return \think\response\Json
 */
function json_page_response($data){
    return json(['code'=>0,'msg'=>'获取成功','count'=>$data['total'],'data'=>$data['data']]);
}

/**获取完整的table表名
 * @param 要查找的表名 $name
 * @param null 是否指定table $table
 * @return string|null
 * @throws \think\Exception
 */
function getTable($name,$table=null){
    if($table){
        return $table;
    }
    if(session('modular')){
        return session('modular').'_'.$name;
    }elseif (request()->module()){
        return request()->module().'_'.$name;
    }else{
        throw new \think\Exception('请先选择表');
    }
}